package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exa.domain.ExaBitch;
import com.ruoyi.exa.domain.ExaBitchUser;
import com.ruoyi.exa.service.IExaBitchService;
import com.ruoyi.exa.service.IExaBitchUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("ExaBitchTask")
public class ExaBitchTask {

    @Autowired
    private IExaBitchService iExaBitchService;

    @Autowired
    private IExaBitchUserService iExaBitchUserService;

    public void bitchStatusUpdate(){
        List<ExaBitch> exaBitchList = iExaBitchService.selectExaBitchList(null);
        for (ExaBitch exaBitch:exaBitchList){
            ExaBitchUser exaBitchUser = new ExaBitchUser();
            exaBitchUser.setBitchId(exaBitch.getId());
            List<ExaBitchUser> exaBitchUsers = iExaBitchUserService.selectExaBitchUserList(exaBitchUser);
            //当前时间
            Date nowDate = DateUtils.getNowDate();
            int compareToStart = nowDate.compareTo(exaBitch.getStartTime());
            int compareToEnd = nowDate.compareTo(exaBitch.getEndTime());
            if(compareToStart<0){
                //比开始时间小 0未开始
                exaBitch.setStatus("0");
            }else if(compareToEnd>0){
                //比结束时间大 1已结束
                exaBitch.setStatus("1");
                for (ExaBitchUser bitchUser:exaBitchUsers){
                    if(!bitchUser.getStatus().equals("2")&&!bitchUser.getStatus().equals("3")){
                        bitchUser.setStatus("4");
                        iExaBitchUserService.updateExaBitchUser(bitchUser);
                    }
                }
            }else if(compareToStart>=0&&compareToEnd<=0&&!exaBitch.getStatus().equals("1")){
                //处于开始结束之间 2进行中
                exaBitch.setStatus("2");
            }
            iExaBitchService.updateExaBitch(exaBitch);
        }
    }
}
